﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Newtonsoft.Json;
using System;
using Newtonsoft.Json.Linq;

namespace Utility.Json
{
	public class Vector2IntConverter : JsonConverter<Vector2Int>
	{
		public override Vector2Int ReadJson (JsonReader reader, Type objectType, Vector2Int existingValue, bool hasExistingValue, JsonSerializer serializer)
		{
			JObject obj = JObject.Load (reader);
			int x = obj[nameof (Vector2Int.x)].Value<int> ();
			int y = obj[nameof (Vector2Int.y)].Value<int> ();
			return new Vector2Int (x, y);
		}

		public override void WriteJson (JsonWriter writer, Vector2Int value, JsonSerializer serializer)
		{
			writer.WriteStartObject ();
			writer.WriteProperty (nameof (value.x), value.x);
			writer.WriteProperty (nameof (value.y), value.y);
			writer.WriteEndObject ();
		}
	}
}